//************************************************************************
/// <copyrigth>2024-2025 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// Cross-Platform event notification which can helpe different modules communication
///</describe>
/// <date>2024/4/28</date>
///***********************************************************************
#ifndef BASEEVENT_H
#define BASEEVENT_H

#include <mutex>
#include <vector>
#include "IBaseEvent.h"
#include "ModuleLoader/ModuleConfig.h"

namespace SystemTool
{
    class CLASS_ITEM BaseEvent : public IBaseEvent
    {
    public:
        // Construct the obj
        BaseEvent();

        // Destruct the obj
        virtual ~BaseEvent();

    private:
        // Copy construct
        BaseEvent(const BaseEvent& other) = delete;

        // Assignment
        BaseEvent& operator=(const BaseEvent& other) = delete;

    public:
        // Register the event
        virtual void Register(int iEventId, std::function<void(EventArgs &)> fEventHandlerFunc);

        // Unregister the event
        virtual void UnRegister(int iEventId, std::function<void(EventArgs &)> fEventHandlerFunc); 

        // Notify the target
        virtual void Notify(EventArgs &Args);

        // Asyn notify the event
        virtual void AsynNotify(EventArgs &Args, std::function<void(EventArgs &)> fResFunc = nullptr, long lTimeout_Ms = 60000);

    private:
        // Add to table
        void Add(void *pData);

        // Remove from the table
        void Remove(void *pData);

        // Clear the table
        void Clear();

    private:
        // Poco event
        void* m_pEvent;

        // Lock for the table
        std::mutex* m_pLock;

        // Target table
        std::vector<void*>* m_pTargetTable;
    };
}

#endif // BASEEVENT_H
